home *** CD-ROM | disk | FTP | other *** search
-
- namespace eval tile {
- namespace eval entry {
- variable State
-
- set State(x) 0
- set State(selectMode) char
- set State(anchor) 0
- set State(scanX) 0
- set State(scanIndex) 0
- set State(scanMoved) 0
-
- set State(scanNum) 1
- set State(scanDen) 1
- set State(deadband) 3 ;# #pixels for mouse-moved deadband.
- }
- }
-
-
- bind TEntry <<Cut>> { tile::entry::Cut %W }
- bind TEntry <<Copy>> { tile::entry::Copy %W }
- bind TEntry <<Paste>> { tile::entry::Paste %W }
- bind TEntry <<Clear>> { tile::entry::Clear %W }
-
- bind TEntry <ButtonPress-1> { tile::entry::Press %W %x }
- bind TEntry <Shift-ButtonPress-1> { tile::entry::Shift-Press %W %x }
- bind TEntry <Double-ButtonPress-1> { tile::entry::Select %W %x word }
- bind TEntry <Triple-ButtonPress-1> { tile::entry::Select %W %x line }
- bind TEntry <B1-Motion> { tile::entry::Drag %W %x }
-
- bind TEntry <B1-Leave> { tile::Repeatedly tile::entry::AutoScroll %W }
- bind TEntry <B1-Enter> { tile::CancelRepeat }
- bind TEntry <ButtonRelease-1> { tile::CancelRepeat }
-
- bind TEntry <Control-ButtonPress-1> {
- %W instate {!readonly !disabled} { %W icursor @%x ; focus %W }
- }
-
- bind TEntry <ButtonPress-2> { tile::entry::ScanMark %W %x }
- bind TEntry <B2-Motion> { tile::entry::ScanDrag %W %x }
- bind TEntry <ButtonRelease-2> { tile::entry::ScanRelease %W %x }
- bind TEntry <<PasteSelection>> { tile::entry::ScanRelease %W %x }
-
- bind TEntry <Key-Left> { tile::entry::Move %W prevchar }
- bind TEntry <Key-Right> { tile::entry::Move %W nextchar }
- bind TEntry <Control-Key-Left> { tile::entry::Move %W prevword }
- bind TEntry <Control-Key-Right> { tile::entry::Move %W nextword }
- bind TEntry <Key-Home> { tile::entry::Move %W home }
- bind TEntry <Key-End> { tile::entry::Move %W end }
-
- bind TEntry <Shift-Key-Left> { tile::entry::Extend %W prevchar }
- bind TEntry <Shift-Key-Right> { tile::entry::Extend %W nextchar }
- bind TEntry <Shift-Control-Key-Left> { tile::entry::Extend %W prevword }
- bind TEntry <Shift-Control-Key-Right> { tile::entry::Extend %W nextword }
- bind TEntry <Shift-Key-Home> { tile::entry::Extend %W home }
- bind TEntry <Shift-Key-End> { tile::entry::Extend %W end }
-
- bind TEntry <Control-Key-slash> { %W selection range 0 end }
- bind TEntry <Control-Key-backslash> { %W selection clear }
-
- bind TEntry <<TraverseIn>> { %W selection range 0 end; %W icursor end }
-
- bind TEntry <KeyPress> { tile::entry::Insert %W %A }
- bind TEntry <Key-Delete> { tile::entry::Delete %W }
- bind TEntry <Key-BackSpace> { tile::entry::Backspace %W }
-
- bind TEntry <Alt-KeyPress> {# nothing}
- bind TEntry <Meta-KeyPress> {# nothing}
- bind TEntry <Control-KeyPress> {# nothing}
- bind TEntry <Key-Escape> {# nothing}
- bind TEntry <Key-Return> {# nothing}
- bind TEntry <Key-KP_Enter> {# nothing}
- bind TEntry <Key-Tab> {# nothing}
-
- if {[tk windowingsystem] eq "aqua"} {
- bind TEntry <Command-KeyPress> {# nothing}
- }
-
- bind TEntry <Control-Key-a> { tile::entry::Move %W home }
- bind TEntry <Control-Key-b> { tile::entry::Move %W prevchar }
- bind TEntry <Control-Key-d> { tile::entry::Delete %W }
- bind TEntry <Control-Key-e> { tile::entry::Move %W end }
- bind TEntry <Control-Key-f> { tile::entry::Move %W nextchar }
- bind TEntry <Control-Key-h> { tile::entry::Backspace %W }
- bind TEntry <Control-Key-k> { %W delete insert end }
-
-
- proc tile::entry::EntrySelection {w} {
- set entryString [string range [$w get] [$w index sel.first] [expr {[$w index sel.last] - 1}]]
- if {[$w cget -show] ne ""} {
- return [string repeat [string index [$w cget -show] 0] [string length $entryString]]
- }
- return $entryString
- }
-
- proc tile::entry::Paste {w} {
- catch {
- set clipboard [::tk::GetSelection $w CLIPBOARD]
- PendingDelete $w
- $w insert insert $clipboard
- See $w insert
- }
- }
-
- proc tile::entry::Copy {w} {
- if {![catch {EntrySelection $w} selection]} {
- clipboard clear -displayof $w
- clipboard append -displayof $w $selection
- }
- }
-
- proc tile::entry::Clear {w} {
- catch { $w delete sel.first sel.last }
- }
-
- proc tile::entry::Cut {w} {
- Copy $w; Clear $w
- }
-
-
- proc tile::entry::ClosestGap {w x} {
- set pos [$w index @$x]
- set bbox [$w bbox $pos]
- if {$x - [lindex $bbox 0] > [lindex $bbox 2]/2} {
- incr pos
- }
- return $pos
- }
-
- proc tile::entry::See {w {index insert}} {
- set c [$w index $index]
- if {$c < [$w index @0] || $c >= [$w index @[winfo width $w]]} {
- $w xview $c
- }
- }
-
- set ::tile::entry::State(startNext) [string equal $tcl_platform(platform) "windows"]
-
- proc tile::entry::NextWord {w start} {
- variable State
- set pos [tcl_endOfWord [$w get] [$w index $start]]
- if {$pos >= 0 && $State(startNext)} {
- set pos [tcl_startOfNextWord [$w get] $pos]
- }
- if {$pos < 0} {
- return end
- }
- return $pos
- }
-
- proc tile::entry::PrevWord {w start} {
- set pos [tcl_startOfPreviousWord [$w get] [$w index $start]]
- if {$pos < 0} {
- return 0
- }
- return $pos
- }
-
- proc tile::entry::RelIndex {w where {index insert}} {
- switch -- $where {
- prevchar { expr {[$w index $index] - 1} }
- nextchar { expr {[$w index $index] + 1} }
- prevword { PrevWord $w $index }
- nextword { NextWord $w $index }
- home { return 0 }
- end { $w index end }
- default { error "Bad relative index $index" }
- }
- }
-
- proc tile::entry::Move {w where} {
- $w icursor [RelIndex $w $where]
- $w selection clear
- See $w insert
- }
-
-
- proc tile::entry::ExtendTo {w index} {
- set index [$w index $index]
- set insert [$w index insert]
-
- if {![$w selection present]} {
- set anchor $insert
- } else {
- set selfirst [$w index sel.first]
- set sellast [$w index sel.last]
-
- if { ($index < $selfirst)
- || ($insert == $selfirst && $index <= $sellast)
- } {
- set anchor $sellast
- } else {
- set anchor $selfirst
- }
- }
-
- if {$anchor < $index} {
- $w selection range $anchor $index
- } else {
- $w selection range $index $anchor
- }
-
- $w icursor $index
- return $anchor
- }
-
- proc tile::entry::Extend {w where} {
- ExtendTo $w [RelIndex $w $where]
- See $w
- }
-
-
- proc tile::entry::Press {w x} {
- variable State
-
- $w icursor [ClosestGap $w $x]
- $w selection clear
- $w instate {!readonly !disabled} { focus $w }
-
- set State(x) $x
- set State(selectMode) char
- set State(anchor) [$w index insert]
- }
-
- proc tile::entry::Shift-Press {w x} {
- variable State
-
- focus $w
- set anchor [ExtendTo $w @$x]
-
- set State(x) $x
- set State(selectMode) char
- set State(anchor) $anchor
- }
-
- proc tile::entry::Select {w x mode} {
- variable State
- set cur [ClosestGap $w $x]
-
- switch -- $mode {
- word { WordSelect $w $cur $cur }
- line { LineSelect $w $cur $cur }
- char { # no-op }
- }
-
- set State(anchor) $cur
- set State(selectMode) $mode
- }
-
- proc tile::entry::Drag {w x} {
- variable State
- set State(x) $x
- DragTo $w $x
- }
-
- proc tile::entry::DragTo {w x} {
- variable State
-
- set cur [ClosestGap $w $x]
- switch $State(selectMode) {
- char { CharSelect $w $State(anchor) $cur }
- word { WordSelect $w $State(anchor) $cur }
- line { LineSelect $w $State(anchor) $cur }
- }
- }
-
- proc tile::entry::AutoScroll {w} {
- variable State
- if {![winfo exists $w]} return
- set x $State(x)
- if {$x > [winfo width $w]} {
- $w xview scroll 2 units
- DragTo $w $x
- } elseif {$x < 0} {
- $w xview scroll -2 units
- DragTo $w $x
- }
- }
-
- proc tile::entry::CharSelect {w from to} {
- if {$to <= $from} {
- $w selection range $to $from
- } else {
- $w selection range $from $to
- }
- $w icursor $to
- }
-
- proc tile::entry::WordSelect {w from to} {
- if {$to < $from} {
- set first [WordBack [$w get] $to]
- set last [WordForward [$w get] $from]
- $w icursor $first
- } else {
- set first [WordBack [$w get] $from]
- set last [WordForward [$w get] $to]
- $w icursor $last
- }
- $w selection range $first $last
- }
-
- proc WordBack {text index} {
- if {[set pos [tcl_wordBreakBefore $text $index]] < 0} { return 0 }
- return $pos
- }
- proc WordForward {text index} {
- if {[set pos [tcl_wordBreakAfter $text $index]] < 0} { return end }
- return $pos
- }
-
- proc tile::entry::LineSelect {w _ _} {
- variable State
- $w selection range 0 end
- $w icursor end
- }
-
-
- proc tile::entry::ScanMark {w x} {
- variable State
- set State(scanX) $x
- set State(scanIndex) [$w index @0]
- set State(scanMoved) 0
- }
-
- proc tile::entry::ScanDrag {w x} {
- variable State
-
- set dx [expr {$State(scanX) - $x}]
- if {abs($dx) > $State(deadband)} {
- set State(scanMoved) 1
- }
- set left [expr {$State(scanIndex) + ($dx*$State(scanNum))/$State(scanDen)}]
- $w xview $left
-
- if {$left != [set newLeft [$w index @0]]} {
- set State(scanX) $x
- set State(scanIndex) $newLeft
- }
- }
-
- proc tile::entry::ScanRelease {w x} {
- variable State
- if {!$State(scanMoved)} {
- $w instate {!disabled !readonly} {
- $w icursor [ClosestGap $w $x]
- catch {$w insert insert [::tk::GetSelection $w PRIMARY]}
- }
- }
- }
-
-
- proc tile::entry::PendingDelete {w} {
- if {[$w selection present]} {
- $w icursor sel.first
- $w delete sel.first sel.last
- return 1
- }
- return 0
- }
-
- proc tile::entry::Insert {w s} {
- if {$s eq ""} { return }
- PendingDelete $w
- $w insert insert $s
- See $w insert
- }
-
- proc tile::entry::Backspace {w} {
- if {[PendingDelete $w]} {
- See $w
- return
- }
- set x [expr {[$w index insert] - 1}]
- if {$x < 0} { return }
-
- $w delete $x
-
- if {[$w index @0] >= [$w index insert]} {
- set range [$w xview]
- set left [lindex $range 0]
- set right [lindex $range 1]
- $w xview moveto [expr {$left - ($right - $left)/2.0}]
- }
- }
-
- proc tile::entry::Delete {w} {
- if {![PendingDelete $w]} {
- $w delete insert
- }
- }
-
- proc tile::entry::compat {w cmd subcmd args} {
- switch -glob -- $cmd-$subcmd {
- sc*-m* { ScanMark $w [lindex $args 0] ;# $w scan mark $x }
- sc*-d* { ScanDrag $w [lindex $args 0] ;# $w scan dragto $x }
- se*-t* {
- CharSelect $w [$w index anchor] [$w index [lindex $args 0]]
- }
- se*-a* {
- ExtendTo $w [lindex $args 0]
- }
- }
- }
-
-